package com.bw.employee.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bw.employee.mapper.TbEmployeeMapper;
import com.bw.po.TbEmployee;
import com.bw.po.TbSection;
import com.bw.employee.service.TbSectionService;
import com.bw.employee.mapper.TbSectionMapper;
import com.bw.result.ResponseState;
import com.bw.result.ResultVo;
import com.bw.util.SnowflakeIdWorker;
import com.bw.vo.IdVo;
import com.bw.vo.TbSectionSearchVo;
import com.bw.vo.TbSectionVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
* @author 花开Mystic
* @description 针对表【tb_section(部门管理)】的数据库操作Service实现
* @createDate 2023-11-10 21:01:05
*/
@Service
public class TbSectionServiceImpl extends ServiceImpl<TbSectionMapper, TbSection>
    implements TbSectionService{

    @Autowired
    private TbSectionMapper tbSectionMapper;

    @Autowired
    private TbEmployeeMapper tbEmployeeMapper;

    private SnowflakeIdWorker idWorker = new SnowflakeIdWorker(0,0);

    private static final String upperCaseStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    private static final String lowerCaseStr = "abcdefghijklmnopqrstuvwxyz";

    @Override
    public ResultVo add(TbSectionVo tbSectionVo) {
        //--1 先生成部门
        TbSection tbSection = new TbSection();
        BeanUtils.copyProperties(tbSectionVo,tbSection);
        boolean save = save(tbSection);

        //--2 部门创建后 开始向员工表中添加负责人的相应信息
        TbEmployee tbEmployee = new TbEmployee();
        tbEmployee.setId(String.valueOf(idWorker.nextId()));
        tbEmployee.setName(tbSection.getPrincipal());
        tbEmployee.setSectionId(tbSection.getId());
        tbEmployee.setMobile(tbSection.getMobile());
        tbEmployee.setJob(tbSection.getJob());
        //--3 自动生成员工工号
        String jobCart = RandomUtil.randomNumbers(6);
        tbEmployee.setJobCart(jobCart);
        //--4 随机生成员工密码
        String password = RandomUtil.randomString(upperCaseStr+lowerCaseStr,4) + RandomUtil.randomNumbers(4);
        tbEmployee.setPassword(password);
        int insert = tbEmployeeMapper.insert(tbEmployee);
        return (save && insert==1)?ResultVo.SUCCESS():ResultVo.FAILED(ResponseState.ERROR);
    }

    @Override
    public ResultVo updateByVo(TbSectionVo tbSectionVo) {
        //根据id查询数据库中部门信息 不存在则返回
        QueryWrapper<TbSection> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(TbSection::getId,tbSectionVo.getId());
        TbSection section = tbSectionMapper.selectOne(wrapper);
        if(section == null){
            return ResultVo.FAILED(ResponseState.NOT_EXIST);
        }
        TbSection tbSection = new TbSection();
        BeanUtils.copyProperties(tbSectionVo,tbSection);
        boolean update = updateById(tbSection);
        return update?ResultVo.SUCCESS():ResultVo.FAILED(ResponseState.ERROR);
    }

    @Override
    public ResultVo delete(IdVo idVo) {
        //--1 判断部门是否存在
        QueryWrapper<TbSection> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TbSection::getId,idVo.getId());
        TbSection one = tbSectionMapper.selectOne(queryWrapper);
        if(one == null){
            return ResultVo.FAILED(ResponseState.NOT_EXIST);
        }
        //--2 删除部门
        boolean remove = removeById(idVo.getId());
        //--3 获取相应部门的员工信息
        if(remove){
            QueryWrapper<TbEmployee> wrapper = new QueryWrapper<>();
            wrapper.lambda().eq(TbEmployee::getSectionId,idVo.getId());
            List<TbEmployee> list = tbEmployeeMapper.selectList(wrapper);
            //--3 置空这些员工的部门信息
            for (TbEmployee employee : list) {
                employee.setSectionId(0); // 0代表当前员工无部门
                tbEmployeeMapper.updateById(employee);
            }
        }
        return remove?ResultVo.SUCCESS():ResultVo.FAILED(ResponseState.ERROR);
    }

    @Override
    public ResultVo permissionSet(TbSectionVo tbSectionVo) {
        TbSection tbSection = new TbSection();
        BeanUtils.copyProperties(tbSectionVo,tbSection);
        boolean update = updateById(tbSection);
        return update?ResultVo.SUCCESS():ResultVo.FAILED(ResponseState.ERROR);
    }

    @Override
    public ResultVo listBySearchVo(TbSectionSearchVo tbSectionSearchVo) {
        Page<TbSection> page = new Page<>(tbSectionSearchVo.getPageNum(), tbSectionSearchVo.getPageSize());
        Page<TbSectionVo> sectionVoPage = tbSectionMapper.listBySearchVo(page,tbSectionSearchVo);
        return ResultVo.SUCCESS(sectionVoPage);
    }

    @Override
    public ResultVo sectionList() {
        QueryWrapper<TbSection> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(TbSection::getDeleted,1);
        List<TbSection> list = tbSectionMapper.selectList(wrapper);
        List<TbSectionVo> voList = list.stream().map(item -> {
            TbSectionVo tbSectionVo = new TbSectionVo();
            tbSectionVo.setId(item.getId());
            tbSectionVo.setName(item.getName());
            return tbSectionVo;
        }).collect(Collectors.toList());
        return ResultVo.SUCCESS(voList);
    }
}




